package com.heba.greenmulu;

import android.os.Bundle;
import androidx.viewpager.widget.PagerAdapter;

import androidx.viewpager.widget.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.chrisbanes.photoview.PhotoView;
import com.gyf.immersionbar.ImmersionBar;
import com.heba.banner.ViewPagerFocusView;
import com.heba.bean.PhotoObject;
import com.heba.config.CommonConfig;
import com.heba.utils.GlideUtil;
import com.heba.view.HackyViewPager;

import java.util.ArrayList;
import java.util.List;

/**
 * 查看大图页面
 * <p>
 * Created by jason on 2019/2/27.
 */

public class ZoomPhotoActivity extends BaseActivity {

    private ImageView img_back;
    private HackyViewPager viewPager;
    private ViewPagerFocusView focusView;
    private TextView txt_tips;
    private PhotoViewPagerAdapter pagerAdapter;

    private List<PhotoObject> list = new ArrayList<>();
    private int position;   //图片位置
    private int type = 0;  //0表示顯示提示文字 1表示不顯示提示文字

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zoom_photo);
        /**
         * 设置狀態欄顏色
         */
        ImmersionBar.with(this)
                .statusBarColor(R.color.colorTextLogIn)
                .init();
        findViewById();
        initView();
        showPagerPhoto();
    }

    @Override
    protected void findViewById() {
        img_back = findViewById(R.id.img_back);
        viewPager = findViewById(R.id.zoom_viewpager);
        focusView = findViewById(R.id.focusview);
        txt_tips = findViewById(R.id.txt_tips);
    }

    @Override
    protected void initView() {
        img_back.setOnClickListener(v -> finishActivityWithFadeInAnimation());

        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            position = bundle.getInt(CommonConfig.POSITION, 0);
            type = bundle.getInt(CommonConfig.TYPE, 0);
            list = (List<PhotoObject>) bundle.getSerializable(CommonConfig.PHOTOLIST);
        }

        /**
         * 显示提示文字
         */
        if (type == 1) {
            txt_tips.setVisibility(View.GONE);
        }
    }

    /**
     * viewpager显示图片
     */
    private void showPagerPhoto() {
        focusView.setCount(list.size(), R.drawable.icon_diary_slideshow_u, R.drawable.icon_diary_slideshow_s);
        pagerAdapter = new PhotoViewPagerAdapter();
        viewPager.setAdapter(pagerAdapter);
        //设置viewpager图片监听器
        viewPager.addOnPageChangeListener(new GuidePageChangeListener());
        viewPager.setCurrentItem(position);
    }

    /**
     * 广告滑动监听
     */
    private class GuidePageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }

        @Override
        public void onPageSelected(final int position) {
            focusView.setCurrentIndex(position);
        }
    }

    /**
     * PhotoViewPagerAdapter适配器
     */
    class PhotoViewPagerAdapter extends PagerAdapter {

        public PhotoViewPagerAdapter() {

        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public View instantiateItem(ViewGroup container, int position) {
            PhotoView photoView = new PhotoView(container.getContext());
            GlideUtil.loadImageWithDefaultPlaceHolder(ZoomPhotoActivity.this, list.get(position).getImageUrl(), photoView);
            photoView.setOnClickListener(v -> finishActivityWithFadeInAnimation());
            // Now just add PhotoView to ViewPager and return it
            container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            return photoView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finishActivityWithFadeInAnimation();
    }
}
